package com.iweb.day10;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * @author WuYongheng
 * @date 2021/11/22
 * @description
 */
public class Messages {
    public static void main(String args[]) {
        // from 来自
        String from = "中国移动";
        // code 验证码
        String code = "";
        for (int j = 0; j < 4; j++) {
            Random random = new Random();
            int i = random.nextInt(10);
            code = code + i;
        }

        // minute 有效时间
        int minute = 5;
        String messages = "【" + from + "】" + "验证码" + code + "，有效期" + minute + "分钟";
        String path = "D:\\scode.txt";
        OutputStream outputStream = null;
        File file = new File(path);
        try {
            byte[] bytes = messages.getBytes(StandardCharsets.UTF_8);
            outputStream = new FileOutputStream(file);
            outputStream.write(bytes);
            System.out.println("成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
